package com.example.dell.fastshopping.mall.model;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.mall.entity.HeadTab;
import com.example.dell.fastshopping.mall.entity.MallEntity;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${YSH} on 2019/4/11.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class DuoBaoMallViewModel extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<MallEntity> mallLists = new MutableLiveData<>();
    private MutableLiveData<List<HeadTab>> head = new MutableLiveData<>();


    public LiveData<MallEntity> getItemList(int id, int pageNum, int pageSize, int status) {
        RetrofitClient.execute(service.getAuctionClassList(id, pageNum, pageSize, status), new NetCallback<BaseResponse<MallEntity>>() {
            @Override
            public void netCallback(BaseResponse<MallEntity> response) {
                if (response.getCode() == 200) {
                    mallLists.setValue(response.getData());
                }
            }
        });
        return mallLists;
    }


    public String[] getTimeArray(long millis) {

        long time = millis / 1000;
        if (time <= 0) {
            return new String[]{"00", "00", "00"};
        }

        String[] str;
        int hour = 0;
        int minute = 0;
        int second = 0;
        minute = (int) time / 60;
        if (minute < 60) {
            second = (int) (time % 60);
            str = new String[]{"00", unitFormat(minute), unitFormat(second)};
        } else {
            hour = minute / 60;
            if (hour > 99)
                return new String[]{"99", "59", "59"};
            minute = minute % 60;
            second = (int) (time - hour * 3600 - minute * 60);
            str = new String[]{unitFormat(hour), unitFormat(minute), unitFormat(second)};
        }
        return str;
    }

    public String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public MutableLiveData<List<HeadTab>> getHeadTable() {
        RetrofitClient.execute(service.getHeadTable(), new NetCallback<BaseResponse<List<HeadTab>>>() {
            @Override
            public void netCallback(BaseResponse<List<HeadTab>> o) {
                if (o.isSuccess()) {
                    head.setValue(o.getData());
                }
            }
        });
        return head;
    }
}
